Logic optimization